/*     */ package com.zimbra.soap;
/*     */ 
/*     */ import com.zimbra.common.account.Key.ServerBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.JSONElement;
/*     */ import com.zimbra.common.soap.HeaderConstants;
/*     */ import com.zimbra.common.soap.SoapHttpTransport;
/*     */ import com.zimbra.common.soap.SoapProtocol;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.AuthTokenException;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.httpclient.URLUtil;
/*     */ import java.io.IOException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ProxyTarget
/*     */ {
/*     */   private final Server mServer;
/*     */   private final AuthToken mAuthToken;
/*     */   private final String mURL;
/*  51 */   private int mMaxAttempts = 0;
/*  52 */   private long mTimeout = -1L;
/*     */   
/*     */   public ProxyTarget(String serverId, AuthToken authToken, HttpServletRequest req) throws ServiceException {
/*  55 */     Provisioning prov = Provisioning.getInstance();
/*  56 */     this.mServer = prov.get(Key.ServerBy.id, serverId);
/*  57 */     if (this.mServer == null) {
/*  58 */       throw AccountServiceException.NO_SUCH_SERVER(serverId);
/*     */     }
/*  60 */     this.mAuthToken = AuthToken.getCsrfUnsecuredAuthToken(authToken);
/*     */     
/*  62 */     String requestStr = req.getRequestURI();
/*  63 */     String qs = req.getQueryString();
/*  64 */     if (qs != null) {
/*  65 */       requestStr = requestStr + "?" + qs;
/*     */     }
/*  67 */     int localAdminPort = prov.getLocalServer().getIntAttr("zimbraAdminPort", 0);
/*  68 */     String url; String url; if ((!prov.isOfflineProxyServer(this.mServer)) && (req.getLocalPort() == localAdminPort)) {
/*  69 */       url = URLUtil.getAdminURL(this.mServer, requestStr, true);
/*     */     } else {
/*  71 */       url = URLUtil.getServiceURL(this.mServer, requestStr, false);
/*     */     }
/*     */     
/*  74 */     this.mURL = url;
/*     */   }
/*     */   
/*     */   public ProxyTarget(Server server, AuthToken authToken, String url) {
/*  78 */     this.mServer = server;
/*  79 */     this.mAuthToken = AuthToken.getCsrfUnsecuredAuthToken(authToken);
/*  80 */     this.mURL = url;
/*     */   }
/*     */   
/*     */ 
/*     */   public ProxyTarget disableRetries()
/*     */   {
/*  86 */     this.mMaxAttempts = 1;return this;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public ProxyTarget setTimeouts(long timeoutMsecs)
/*     */   {
/*  93 */     this.mTimeout = timeoutMsecs;return this;
/*     */   }
/*     */   
/*     */   public Server getServer() {
/*  97 */     return this.mServer;
/*     */   }
/*     */   
/*     */   public boolean isTargetLocal() throws ServiceException {
/* 101 */     Server localServer = Provisioning.getInstance().getLocalServer();
/* 102 */     return this.mServer.getId().equals(localServer.getId());
/*     */   }
/*     */   
/*     */   public Element dispatch(Element request) throws ServiceException {
/* 106 */     SoapProtocol proto = (request instanceof Element.JSONElement) ? SoapProtocol.SoapJS : SoapProtocol.Soap12;
/* 107 */     SoapHttpTransport transport = new SoapHttpTransport(this.mURL);
/*     */     try {
/* 109 */       transport.setAuthToken(this.mAuthToken.toZAuthToken());
/* 110 */       transport.setRequestProtocol(proto);
/* 111 */       return transport.invokeWithoutSession(request);
/*     */     } catch (IOException e) {
/* 113 */       throw ServiceException.PROXY_ERROR(e, this.mURL);
/*     */     } finally {
/* 115 */       transport.shutdown();
/*     */     }
/*     */   }
/*     */   
/*     */   public Element dispatch(Element request, ZimbraSoapContext zsc) throws ServiceException {
/* 120 */     return (Element)execute(request, zsc).getSecond();
/*     */   }
/*     */   
/*     */   public Pair<Element, Element> execute(Element request, ZimbraSoapContext zsc) throws ServiceException {
/* 124 */     if (zsc == null) {
/* 125 */       return new Pair(null, dispatch(request));
/*     */     }
/* 127 */     SoapProtocol proto = (request instanceof Element.JSONElement) ? SoapProtocol.SoapJS : SoapProtocol.Soap12;
/* 128 */     if ((proto == SoapProtocol.Soap12) && (zsc.getRequestProtocol() == SoapProtocol.Soap11)) {
/* 129 */       proto = SoapProtocol.Soap11;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 136 */     boolean excludeAccountDetails = AccountConstants.CHANGE_PASSWORD_REQUEST.equals(request.getQName());
/* 137 */     Element envelope = proto.soapEnvelope(request, zsc.toProxyContext(proto, excludeAccountDetails));
/*     */     
/* 139 */     SoapHttpTransport transport = null;
/*     */     try {
/* 141 */       transport = new SoapHttpTransport(this.mURL);
/* 142 */       transport.setTargetAcctId(zsc.getRequestedAccountId());
/* 143 */       if (this.mMaxAttempts > 0)
/* 144 */         transport.setRetryCount(this.mMaxAttempts);
/* 145 */       if (this.mTimeout >= 0L) {
/* 146 */         transport.setTimeout((int)Math.min(this.mTimeout, 2147483647L));
/*     */       }
/* 148 */       transport.setResponseProtocol(zsc.getResponseProtocol());
/* 149 */       AuthToken authToken = AuthToken.getCsrfUnsecuredAuthToken(zsc.getAuthToken());
/* 150 */       if ((authToken != null) && (!StringUtil.isNullOrEmpty(authToken.getProxyAuthToken()))) {
/* 151 */         transport.setAuthToken(authToken.getProxyAuthToken());
/*     */       }
/* 153 */       if (ZimbraLog.soap.isDebugEnabled()) {
/* 154 */         ZimbraLog.soap.debug("Proxying request: proxy=%s targetAcctId=%s", new Object[] { toString(), zsc.getRequestedAccountId() });
/*     */       }
/*     */       
/*     */ 
/* 158 */       disableCsrfFlagInAuthToken(envelope, authToken, request.getQName());
/*     */       
/* 160 */       Element response = transport.invokeRaw(envelope);
/* 161 */       Element body = transport.extractBodyElement(response);
/* 162 */       return new Pair(transport.getZimbraContext(), body);
/*     */     } catch (IOException e) {
/* 164 */       throw ServiceException.PROXY_ERROR(e, this.mURL);
/*     */     } finally {
/* 166 */       if (transport != null) {
/* 167 */         transport.shutdown();
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void disableCsrfFlagInAuthToken(Element envelope, AuthToken authToken, QName qName)
/*     */     throws ServiceException
/*     */   {
/* 179 */     Element header = envelope.getOptionalElement("Header");
/* 180 */     if (header != null) {
/* 181 */       Element context = header.getOptionalElement(HeaderConstants.CONTEXT);
/* 182 */       if (context != null) {
/* 183 */         Element token = context.getOptionalElement("authToken");
/* 184 */         if (token != null) {
/*     */           try {
/* 186 */             token.setText(authToken.getEncoded());
/*     */           } catch (AuthTokenException ate) {
/* 188 */             throw ServiceException.PROXY_ERROR(ate, qName.getName());
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 193 */     ZimbraLog.soap.debug("Modified auth token in soap envelope, csrf token flag is disabled. The new envelope : %s", new Object[] { envelope });
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 198 */     return "ProxyTarget(url=" + this.mURL + ")";
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/soap/ProxyTarget.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */